package com.jumi.microservice.stock;

import com.jumi.microservice.common.constant.InventoryRedisKey;
import com.jumi.microservice.domain.GoodsStockDO;
import com.jumi.microservice.domain.GoodsStockLogDO;
import com.jumi.microservice.domain.GoodsWarehouseStockDO;
import com.jumi.microservice.mapper.GoodsStockLogMapper;
import com.jumi.microservice.mapper.GoodsStockMapper;
import com.jumi.microservice.mapper.GoodsWarehouseStockMapper;
import net.sf.cglib.core.Local;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 商品库存更新命令的抽象基类
 *
 * @author Administrator
 */
public abstract class AbstractStockUpdater implements StockUpdater {

    private static final Logger logger = LoggerFactory.getLogger(AbstractStockUpdater.class);

    /**
     * 商品库存DO对象
     */
    protected GoodsStockDO goodsStock;

    /**
     * 商品分仓库存DO对象
     */
    protected GoodsWarehouseStockDO goodsWarehouseStocks;

    /**
     * 商品库存流水DO对象
     */
    protected GoodsStockLogDO goodsStockLog;

    /**
     * 商品库存管理模块的mapper组件
     */
    protected GoodsStockMapper goodsStockMapper;

    /**
     * 商品分仓库存管理模块的mapper组件
     */
    protected GoodsWarehouseStockMapper goodsWarehouseStockMapper;

    /**
     * 商品库存流水管理模块的mapper组件
     */
    protected GoodsStockLogMapper goodsStockLogMapper;

//    @Autowired
//    RedisTemplate redisTemplate;

    /**
     * 构造函数
     *
     * @param goodsStock                商品库存DO对象
     * @param goodsWarehouseStocks      商品分仓库存DO对象
     * @param goodsStockLog             商品库存流水DO对象
     * @param goodsStockMapper          商品库存管理模块的mapper组件
     * @param goodsWarehouseStockMapper 商品分仓库存管理模块的mapper组件
     * @param goodsStockLogMapper       商品库存流水管理模块的mapper组件
     */
    public AbstractStockUpdater(GoodsStockDO goodsStock,
                                GoodsWarehouseStockDO goodsWarehouseStocks,
                                GoodsStockLogDO goodsStockLog,
                                GoodsStockMapper goodsStockMapper,
                                GoodsWarehouseStockMapper goodsWarehouseStockMapper,
                                GoodsStockLogMapper goodsStockLogMapper) {
        this.goodsStock = goodsStock;
        this.goodsWarehouseStocks = goodsWarehouseStocks;
        this.goodsStockLog = goodsStockLog;
        this.goodsStockMapper = goodsStockMapper;
        this.goodsWarehouseStockMapper = goodsWarehouseStockMapper;
        this.goodsStockLogMapper = goodsStockLogMapper;
    }

    /**
     * 更新商品库存
     */
    @Override
    public Boolean updateGoodsStock() {
        try {
            Integer saleStock = updateSaleStockQuantity();
            Integer lockStock = updateLockedStockQuantity();
            Integer saledStock = updateSaledStockQuantity();
            Integer preStock =updatePreStockQuantity();
            Integer skuStock =updateSkuStorageQuantity();

            //返回skuid和仓库id
            Long skuId = executeUpdateGoodsStock();
            Long wareId = executeUpdateWarehouseGoodsStock();
            //log暂时不处理
//            executeUpdateGoodsStockLog();
            //20210108 同步redis 线上暂时注释
//            if(redisTemplate.hasKey(InventoryRedisKey.GOODS_SKU_STOCK+skuId)) {
//                if (lockStock != null) {
//                    redisSkuStockInc(InventoryRedisKey.GOODS_SKU_STOCK + skuId, InventoryRedisKey.LOCKEDSTORAGE, lockStock);
//                }
//                if (saledStock != null) {
//                    redisSkuStockInc(InventoryRedisKey.GOODS_SKU_STOCK + skuId, InventoryRedisKey.SALEDSTORAGE, saledStock);
//                }
//                if (preStock != null) {
//                    redisSkuStockInc(InventoryRedisKey.GOODS_SKU_STOCK + skuId, InventoryRedisKey.PRESTORAGE, preStock);
//                }
//                if (skuStock != null) {
//                    redisSkuStockInc(InventoryRedisKey.GOODS_SKU_STOCK + skuId, InventoryRedisKey.GOODSSKUSTORAGE, skuStock);
//                }
//            }
//            if(redisTemplate.hasKey(InventoryRedisKey.GOODS_SKU_WARE_STOCK + skuId + ":" + wareId)){
//                if (saleStock != null && wareId != null) {
//                    redisSkuStockInc(InventoryRedisKey.GOODS_SKU_WARE_STOCK + skuId + ":" + wareId, InventoryRedisKey.SALESTOCKQUANTITY, saleStock);
//                }
//                if (lockStock != null) {
//                    if (wareId != null) {
//                        redisSkuStockInc(InventoryRedisKey.GOODS_SKU_WARE_STOCK + skuId + ":" + wareId, InventoryRedisKey.LOCKEDSTOCKQUANTITY, lockStock);
//                    }
//                }
//                if (saledStock != null) {
//                    if (wareId != null) {
//                        redisSkuStockInc(InventoryRedisKey.GOODS_SKU_WARE_STOCK + skuId + ":" + wareId, InventoryRedisKey.SALEDSTOCKQUANTITY, saledStock);
//                    }
//                }
//            }
        } catch (Exception e) {
            logger.error("error", e);
        }
        return true;
    }

    /**
     * 更新商品的销售库存
     *
     * @throws Exception
     */
    protected abstract Integer updateSaleStockQuantity() throws Exception;

    /**
     * 更新商品的锁定库存
     *
     * @throws Exception
     */
    protected abstract Integer updateLockedStockQuantity() throws Exception;

    /**
     * 更新商品的已销售库存
     *
     * @throws Exception
     */
    protected abstract Integer updateSaledStockQuantity() throws Exception;

    /**
     * 更新商品的预售库存
     *
     * @throws Exception
     */
    protected abstract Integer updatePreStockQuantity() throws Exception;

    /**
     * 更新商品的总库存
     *
     * @throws Exception
     */
    protected abstract Integer updateSkuStorageQuantity() throws Exception;


    /**
     * 实际执行更新商品库存的操作
     *
     * @throws Exception
     */
    private Long executeUpdateGoodsStock() throws Exception {
            goodsStock.setUpdateTime(LocalDateTime.now());
            goodsStockMapper.updateById(goodsStock);
            return goodsStock.getGoodsSkuId();
    }

    /**
     * 实际执行更新商品分仓库存的操作
     *
     * @throws Exception
     */
    private Long executeUpdateWarehouseGoodsStock() throws Exception {
        if(goodsWarehouseStocks!=null) {
            goodsWarehouseStocks.setUpdateTime(LocalDateTime.now());
            goodsWarehouseStockMapper.updateById(goodsWarehouseStocks);
            return goodsWarehouseStocks.getWarehouseId();
        }
        return null;
    }

    /**
     * 实际执行更新商品库存流水的操作
     *
     * @throws Exception
     */
    private void executeUpdateGoodsStockLog() throws Exception {
        if(goodsStockLog!=null) {
            goodsStockLog.setUpdateTime(LocalDateTime.now());
            goodsStockLogMapper.updateById(goodsStockLog);
        }
    }

    /**
     * 更新库存的库存值 自增
     * @param key redis的库存key
     * @param mapKey 商品库存的map中key
     * @param stockNum 库存量
     */
//    private void redisSkuStockInc(String key, String mapKey ,Integer stockNum){
//        redisTemplate.opsForHash().increment(key, mapKey, stockNum);
//    }
}
